﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RM2012Service.DataAccess;
using RM2012Service.Business;
using System.Data;

namespace RM2012Service.Controller
{
    public class MenuCtrl
    {
        MenuDAO dao;

        public MenuCtrl()
        {
            dao = new MenuDAO();
        }

        public DataTable GetAllMenuDataTable()
        {
            return dao.GetAllMenu();
        }

        public DataSet GetAllMenuDataSet()
        {
            return dao.GetAllMenuDataSet();
        }

        public MenuInfo[] GetAllMenu()
        {
            MenuInfo[] list = null;
            try
            {
                DataTable dt = dao.GetAllMenu();
                if (dt.Rows.Count > 0)
                {
                    list = new MenuInfo[dt.Rows.Count];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        list[i] = new MenuInfo();
                        list[i].MenuId = dt.Rows[i]["MenuId"].ToString();
                        list[i].MenuName = dt.Rows[i]["MenuName"].ToString();
                        list[i].CateName = dt.Rows[i]["CateName"].ToString();
                        string price = dt.Rows[i]["Price"].ToString();
                        double priceDouble = Convert.ToDouble(price);
                        string priceStr = Convert.ToString(priceDouble);
                        list[i].Price = priceStr;
                        list[i].UnitName = dt.Rows[i]["UnitName"].ToString();
                        if (dt.Rows[i]["State"].ToString().Equals("1"))
                        {
                            list[i].State = "Còn";
                        } 
                        else
                        {
                            list[i].State = "Hết";
                        }
                        list[i].Remark = dt.Rows[i]["Remark"].ToString();
                    }
                }
            }
            catch
            {
                list = null;
            }
            return list;
        }

        public MenuInfo GetMenuById(string menuId)
        {
            MenuInfo info = null;
            try
            {
                DataTable dt = dao.GetMenuById(menuId);
                if (dt.Rows.Count > 0)
                {
                    info = new MenuInfo();
                    info.MenuId = dt.Rows[0]["MenuId"].ToString();
                    info.MenuName = dt.Rows[0]["MenuName"].ToString();
                    info.CateName = dt.Rows[0]["CateName"].ToString();
                    string price = dt.Rows[0]["Price"].ToString();
                    double priceDouble = Convert.ToDouble(price);
                    string priceStr = Convert.ToString(priceDouble);
                    info.Price = priceStr;
                    info.UnitName = dt.Rows[0]["UnitName"].ToString();
                    if (dt.Rows[0]["State"].ToString().Equals("1"))
                    {
                        info.State = "Còn";
                    }
                    else
                    {
                        info.State = "Hết";
                    }
                    info.Remark = dt.Rows[0]["Remark"].ToString();
                }
            }
            catch
            {
                info = null;
            }
            return info;
        }
    }
}
